package com.stylefeng.guns.modular.purchase.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.IdGenerator;
import com.stylefeng.guns.modular.dto.ReturnGoodsAddRequest;
import com.stylefeng.guns.modular.dto.ReturnListQueryRequest;
import com.stylefeng.guns.modular.purchase.service.IGoodsReturnListService;
import com.stylefeng.guns.modular.system.model.GoodsReturnList;
import com.stylefeng.guns.modular.system.model.Purchase;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.model.GoodsReturn;
import com.stylefeng.guns.modular.purchase.service.IGoodsReturnService;

import java.util.Date;
import java.util.List;

/**
 * 退货出库控制器
 *
 * @author fengshuonan
 * @Date 2018-09-12 20:43:36
 */
@Controller
@RequestMapping("/goodsReturn")
public class GoodsReturnController extends BaseController {

    private String PREFIX = "/purchase/goodsReturn/";

    @Autowired
    private IGoodsReturnService goodsReturnService;
    @Autowired
    private IGoodsReturnListService goodsReturnListService;

    /**
     * 跳转到退货出库首页
     */
    @RequestMapping("")
    public String index(Model model) {
        String orderNumber = "TH" + DateUtil.formatDate(new Date(), "yyyyMMddHHmmss");
        model.addAttribute("returnOrderNumber", orderNumber);
        return PREFIX + "goodsReturn.html";
    }

    /**
     * 跳转到添加退货出库
     */
    @RequestMapping("/goodsReturn_add")
    public String goodsReturnAdd() {
        return PREFIX + "goodsReturn_add.html";
    }

    /**
     * 跳转到修改退货出库
     */
    @RequestMapping("/goodsReturn_update/{goodsReturnId}")
    public String goodsReturnUpdate(@PathVariable Integer goodsReturnId, Model model) {
        GoodsReturn goodsReturn = goodsReturnService.selectById(goodsReturnId);
        model.addAttribute("item", goodsReturn);
        LogObjectHolder.me().set(goodsReturn);
        return PREFIX + "goodsReturn_edit.html";
    }

    /**
     * 获取退货出库列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(ReturnListQueryRequest request) {
        Page<GoodsReturn> page = new PageFactory<GoodsReturn>().defaultPage();
        Page<GoodsReturn> resultPage = goodsReturnService.selectReturnPageAndDataScope(page, request);
        PageInfoBT<GoodsReturn> pageInfoBT = this.packForBT(resultPage);
        return pageInfoBT;
    }

    /**
     * 新增退货出库
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@RequestBody ReturnGoodsAddRequest request) {
        boolean success = goodsReturnService.insertListAndGoods(request);
        if (success) {
            String orderNumber = "TH" + DateUtil.formatDate(new Date(), "yyyyMMddHHmmss");
            return SUCCESS_TIP.setResultData(orderNumber);
        }
        return new ErrorTip(500, "保存失败, 请检查参数!");
    }

    /**
     * 删除退货出库
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam(value = "goodsReturnListId") Long goodsReturnListId) {
        boolean success = goodsReturnService.deleteOrderAndGoods(goodsReturnListId);
        if (success) {
            return SUCCESS_TIP;
        }
        return new ErrorTip(500, "删除失败，请稍候再试!");
    }

    /**
     * 修改退货出库
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(GoodsReturn goodsReturn) {
        goodsReturnService.updateById(goodsReturn);
        return SUCCESS_TIP;
    }

    /**
     * 退货出库详情
     */
    @RequestMapping(value = "/detail/{goodsReturnId}")
    @ResponseBody
    public Object detail(@PathVariable("goodsReturnId") Integer goodsReturnId) {
        return goodsReturnService.selectById(goodsReturnId);
    }
}
